/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System System Specific Module                         */
/*                                                                           */
/*    Copyright (C) 2010 Laszlo Arvai                                        */
/*                                                                           */
/*    ------------------------------------------------------------------     */
/*    netDOAP - Domino OS Announce Protocol                                  */
/*****************************************************************************/

//! \defgroup ethernet Ethernet Announce

/*****************************************************************************/
/* Includes                                                                  */
/*****************************************************************************/
#include <krnlSystemTimer.h>
#include <netPacketBuffer.h>
#include <netNBNSClient.h>
#include <netIP.h>
#include <netUDP.h>
#include <netMAC.h>
#include <netUtils.h>
#include <sysCompiler.h>
#include "sysSettings.h"

#ifdef netDOAP_ENABLED

/*****************************************************************************/
/* Constants                                                                 */
/*****************************************************************************/
#define netANNOUNCE_PORT	30303

/*****************************************************************************/
/* Global variables                                                          */
/*****************************************************************************/
static dosConstString l_announce_header = "DOAP:1.0";
static dosConstString l_ip_header = "\nIP:";
static dosConstString l_name_header = "\nNAME:";
static dosConstString l_mac_header = "\nMAC:";
static dosConstString l_mac_ppp =	"PPP";
static dosConstString l_code_header = "\nCODE:";
static dosConstString l_code_powerup = "1,Announce";
static dosConstString l_code_reply = "2,Reply";

/*********************************************************************
 * Function:        void AnnounceIP(void)
 *
 * PreCondition:    Stack is initialized()
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        AnnounceIP opens a UDP socket and transmits a 
 *					broadcast packet to port 30303.  If a computer is
 *					on the same subnet and a utility is looking for 
 *					packets on the UDP port, it will receive the 
 *					broadcast.  For this application, it is used to 
 *					announce the change of this board's IP address.
 *					The messages can be viewed with the MCHPDetect.exe
 *					program.
 *
 * Note:            A UDP socket must be available before this 
 *					function is called.  It is freed at the end of 
 *					the function.  MAX_UDP_SOCKETS may need to be 
 *					increased if other modules use UDP sockets.
 ********************************************************************/
#define BUFFER_LENGTH 20
void netDOAPSendmessage(void)
{
	dosBool success;
	dosWord length;
	dosWord protocol_header_pos;
	netIPHeaderInfo ip_header;
	MAC_ADDR mac_address;
	dosChar buffer[20];

	// create UDP Announce packet
	success = netCreateTXPacket(netPT_IP);
	length = 0;
	if( success )
	{
		ip_header.Address.Val = IP_ADDR_BROADCAST;
		ip_header.HeaderLength = netIP_HEADER_LENGTH;
		ip_header.PacketLength = netIP_HEADER_LENGTH + netUDP_HEADER_LENGTH;
		ip_header.Protocol = netIPDT_UDP;
		netIPCreateHeader( &ip_header );
	
		protocol_header_pos = netGetTXPacketPos();
	
		success = netCreateUDPHeader( netANNOUNCE_PORT, netANNOUNCE_PORT, length );
	}

	///////////////////////
	// set announce content

	// send announce header
	length += netWriteConstStringTXPacket( l_announce_header, &success );

#ifdef netDOAP_NAME
	// send device name
	length += netWriteConstStringTXPacket( l_name_header, &success );
	length += netWriteStringTXPacket( netDOAP_NAME, &success );
#endif

	// store MAC address
	length += netWriteConstStringTXPacket( l_mac_header, &success );

#if !defined(netPPP_ENABLED)
	netMACGetMACAddress( &mac_address );
#else
	length += netWriteConstStringTXPacket( l_mac_ppp, &success );
#endif

	// store IP address
	netIPAddressToString( buffer, BUFFER_LENGTH, &g_netIPAddress );
	length += netWriteConstStringTXPacket( l_ip_header, &success );
	length += netWriteStringTXPacket( buffer, &success );

	// store NetBIOS name
#ifdef netNBNS_ENABLED
	
#endif

	// store code
	length += netWriteConstStringTXPacket( l_code_header, &success );

	length += netWriteConstStringTXPacket( l_code_powerup, &success );

	// send packet
	if( success )
	{
    netSetTXPacketPos( protocol_header_pos );
    netUpdateUDPHeader( &ip_header, length );
		netSendTXPacket();
	}
	else
	{
		netCancelTXPacket();
	}
}
#undef BUFFER_LENGTH

void netSendAnnounce(void)
{
	static dosBool first = dosTrue;

	if( first )
	{
		netDOAPSendmessage();
		first = dosFalse;
	}
}

#endif